import router from './_'
import pagination from '../lib/pagination'

router.get('/school', pagination, async (ctx) => {
  const page = ctx.state.page
  const data = await ctx.db.School.find(page.query, { name: 1, provinceCode: 1, })
                        .sort(page.sort)
                        .skip((page.page - 1) * page.limit)
                        .limit(page.limit)
  ctx.body = {
    meta: {
      page: page.page,
      limit: page.limit,
      count: await ctx.db.School.count(page.query),
      query: page,
    },
    items: data,
  }
})

router.get('/school/:id', async (ctx) => {
  ctx.body = await ctx.db.School.findById(ctx.params.id)
})
export default router